home *** CD-ROM | disk | FTP | other *** search
/ MacAddict 114 / macaddict114.cdr / Software / Utilities / macam.0.8.4.dmg / macam sources / app_specific / MyImageDocument.m < prev    next >
Encoding:
Text File  |  2005-08-23  |  10.8 KB  |  322 lines

  1. /*
  2.  macam - webcam app and QuickTime driver component
  3.  Copyright (C) 2002 Matthias Krauss (macam@matthias-krauss.de)
  4.  
  5.  This program is free software; you can redistribute it and/or modify
  6.  it under the terms of the GNU General Public License as published by
  7.  the Free Software Foundation; either version 2 of the License, or
  8.  (at your option) any later version.
  9.  
  10.  This program is distributed in the hope that it will be useful,
  11.  but WITHOUT ANY WARRANTY; without even the implied warranty of
  12.  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  13.  GNU General Public License for more details.
  14.  
  15.  You should have received a copy of the GNU General Public License
  16.  along with this program; if not, write to the Free Software
  17.  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
  18.  $Id: MyImageDocument.m,v 1.5 2005/08/23 17:12:14 hxr Exp $
  19.  */
  20.  
  21. #import "MyImageDocument.h"
  22. #import "MyImageWindowController.h"
  23.  
  24. @implementation MyImageDocument
  25.  
  26. - (void) dealloc 
  27. {
  28.     if (imageRep) 
  29.         [imageRep release];
  30. //    imageRep = NULL;
  31.     
  32.     [super dealloc];
  33. }
  34.  
  35. - (void)makeWindowControllers {
  36.     MyImageWindowController* winCon;
  37.     winCon=[[MyImageWindowController alloc] initWithWindowNibName:@"MyImageDocument" owner:self];
  38.     if (winCon) [self addWindowController:winCon];
  39. }
  40.  
  41. - (void)windowControllerDidLoadNib:(NSWindowController *) aController
  42. {
  43.     NSBitmapImageRep* newRep;
  44.     if (deferredOpenImageRep) [deferredOpenImageRep autorelease];
  45.     newRep=deferredOpenImageRep;
  46.     [self setHasUndoManager:NO];
  47.     started=YES;
  48.     
  49.     if (!newRep) {    //In case we shouldn't open anything, take an empty image
  50.         imageRep=[[[NSBitmapImageRep alloc] initWithBitmapDataPlanes:NULL
  51.                                                           pixelsWide:160
  52.                                                           pixelsHigh:120
  53.                                                        bitsPerSample:8
  54.                                                      samplesPerPixel:3
  55.                                                             hasAlpha:NO
  56.                                                             isPlanar:NO
  57.                                                       colorSpaceName:NSDeviceRGBColorSpace
  58.                                                          bytesPerRow:0
  59.                                                         bitsPerPixel:0] autorelease];
  60.     }
  61.     [self setImageRep:newRep];
  62.     [super windowControllerDidLoadNib:aController];
  63. }
  64.  
  65. - (BOOL)shouldRunSavePanelWithAccessoryView {
  66.     return NO;
  67. }
  68.  
  69. - (BOOL) prepareSavePanel:(NSSavePanel*)panel {
  70.     if (![super prepareSavePanel:panel]) return NO;
  71.     [panel setAccessoryView:NULL];
  72.     return YES;
  73. }
  74.  
  75. - (NSData *)dataRepresentationOfType:(NSString *)aType 
  76. {
  77.     NSDictionary* dict=[NSDictionary dictionaryWithObjectsAndKeys:
  78.         [NSNumber numberWithFloat:quality], NSImageCompressionFactor, NULL];
  79.     
  80.     if ([aType isEqualToString:@"JPEG Image"]) 
  81.     {
  82.         return [imageRep representationUsingType:NSJPEGFileType properties:dict];
  83.     } 
  84.     else if ([aType isEqualToString:@"PNG Image"]) 
  85.     {
  86.         return [imageRep representationUsingType:NSPNGFileType properties:dict];
  87.     } 
  88.     else if ([aType isEqualToString:@"BMP Image"]) 
  89.     {
  90.         return [imageRep representationUsingType:NSBMPFileType properties:dict];
  91.     } 
  92.     else if ([aType isEqualToString:@"GIF Image"]) 
  93.     {
  94.         return [imageRep representationUsingType:NSGIFFileType properties:dict];
  95.     } 
  96.     else 
  97.     {
  98.         return [imageRep TIFFRepresentationUsingCompression:NSTIFFCompressionLZW factor:0.0f];
  99.     }
  100. }
  101.  
  102. - (BOOL)loadDataRepresentation:(NSData *)data ofType:(NSString *)aType {
  103.     NSBitmapImageRep* newRep=[[[NSBitmapImageRep alloc] initWithData:data] autorelease];
  104.     if (!newRep) return NO;
  105.     if (!started) {
  106.         [newRep retain];
  107.         deferredOpenImageRep=newRep;
  108.         return YES;
  109.     } else {
  110.         [self setImageRep:newRep];
  111.         return YES;
  112.     }
  113. }
  114.  
  115. - (void) setImageRep:(NSBitmapImageRep*) newRep {
  116.     if (imageRep) [imageRep autorelease];
  117.     imageRep=newRep;
  118.     if (imageRep) [imageRep retain];
  119.     [[NSNotificationCenter defaultCenter] postNotificationName:@"Document changed notification" object:self];
  120. }
  121.  
  122. - (NSBitmapImageRep*) imageRep {
  123.     return imageRep;
  124. }
  125.  
  126.  
  127. /*
  128.  
  129.  Could someone tellme why this doesn't work? It loses color channels on the second rotation...
  130.  
  131. - (void) rotateCW:(id)sender {
  132.     NSBitmapImageRep* newRep;
  133.     CGContextRef ctx;
  134.     CGImageRef img;
  135.     CGColorSpaceRef colorSpace1;
  136.     CGColorSpaceRef colorSpace2;
  137.     CGDataProviderRef dataProvider;
  138.     CGRect rect;
  139.     float decodeArray[]={0.0f,1.0f};
  140.     float black[]={0.0f,0.0f,0.0f,1.0f};
  141.     float white[]={1.0f,1.0f,1.0f,1.0f};
  142.  
  143.     if (!imageRep) return;
  144.  
  145.     newRep=[[[NSBitmapImageRep alloc] initWithBitmapDataPlanes:NULL
  146.                                                       pixelsWide:[imageRep pixelsHigh]
  147.                                                       pixelsHigh:[imageRep pixelsWide]
  148.                                                    bitsPerSample:8
  149.                                                  samplesPerPixel:3
  150.                                                         hasAlpha:NO
  151.                                                         isPlanar:NO
  152.                                                   colorSpaceName:NSDeviceRGBColorSpace
  153.                                                      bytesPerRow:0
  154.                                                     bitsPerPixel:32] autorelease];
  155.     if (!newRep) return;
  156.  
  157.     colorSpace1=CGColorSpaceCreateDeviceRGB();
  158.     colorSpace2=CGColorSpaceCreateDeviceRGB();
  159.  
  160.     dataProvider=CGDataProviderCreateWithData(NULL,
  161.                                               [imageRep bitmapData],
  162.                                               [imageRep pixelsHigh]*[imageRep bytesPerRow],
  163.                                               NULL);
  164.  
  165.     NSLog(@"bpp:%i spp:%i bpr:%i",[newRep bitsPerPixel],[newRep samplesPerPixel],[newRep bytesPerRow]);
  166.     ctx=CGBitmapContextCreate([newRep bitmapData],
  167.                               [newRep pixelsWide],
  168.                               [newRep pixelsHigh],
  169.                               8,
  170.                               [newRep bytesPerRow],
  171.                               colorSpace1,
  172.                               kCGImageAlphaNoneSkipLast);
  173.  
  174.  
  175.     img=CGImageCreate([imageRep pixelsWide],
  176.                       [imageRep pixelsHigh],
  177.                       8,
  178.                       [imageRep bitsPerPixel],
  179.                       [imageRep bytesPerRow],
  180.                       colorSpace2,
  181.                       kCGImageAlphaNone,
  182.                       dataProvider,
  183.                       decodeArray,
  184.                       0,
  185.                       kCGRenderingIntentDefault);
  186.  
  187.     CGDataProviderRelease(dataProvider);
  188.     CGColorSpaceRelease(colorSpace1);
  189.     CGColorSpaceRelease(colorSpace2);
  190.  
  191.     rect=CGRectMake(0,0,[imageRep pixelsWide],[imageRep pixelsHigh]);
  192.  
  193.     CGContextRotateCTM(ctx,-1.5705);
  194.     CGContextTranslateCTM(ctx,-[imageRep pixelsWide],0.0f);
  195.  
  196.     CGContextSetFillColor(ctx,black);
  197.     CGContextSetStrokeColor(ctx,white);
  198.     CGContextFillRect(ctx,rect);
  199.  
  200.     CGContextDrawImage(ctx,rect,img);
  201.     [imageRep drawInRect:NSMakeRect(0,0,[imageRep pixelsWide],[imageRep pixelsHigh])];
  202.     
  203.     CGImageRelease(img);
  204.     CGContextRelease(ctx);
  205.     [self setImageRep:newRep];
  206. }
  207. */
  208.  
  209. - (void) rotateCW:(id)sender {
  210.     NSBitmapImageRep* newRep;
  211.     unsigned char* srcBase;
  212.     unsigned char* dstBase;
  213.     unsigned char* srcRun;
  214.     unsigned char* dstRun;
  215.     long x,y,srcWidth,srcHeight,srcPB,dstPB,srcRB,dstRB;
  216.  
  217.     if (!imageRep) return;
  218.  
  219.     newRep=[[[NSBitmapImageRep alloc] initWithBitmapDataPlanes:NULL
  220.                                                     pixelsWide:[imageRep pixelsHigh]
  221.                                                     pixelsHigh:[imageRep pixelsWide]
  222.                                                  bitsPerSample:8
  223.                                                samplesPerPixel:3
  224.                                                       hasAlpha:NO
  225.                                                       isPlanar:NO
  226.                                                 colorSpaceName:NSDeviceRGBColorSpace
  227.                                                    bytesPerRow:0
  228.                                                   bitsPerPixel:0] autorelease];
  229.  
  230.     if (!newRep) return;
  231.  
  232.     //We go through the source Image row per row. Start 
  233.     
  234.     srcBase=[imageRep bitmapData];
  235.     dstBase=[newRep bitmapData];
  236.  
  237.     srcWidth=[imageRep pixelsWide];
  238.     srcHeight=[imageRep pixelsHigh];
  239.  
  240.     srcPB=[imageRep bitsPerPixel]/8;
  241.     dstPB=[newRep bitsPerPixel]/8;
  242.     
  243.     srcRB=[imageRep bytesPerRow];
  244.     dstRB=[newRep bytesPerRow];
  245.  
  246.     for (y=0;y<srcHeight;y++) {
  247.         srcRun=srcBase+y*srcRB;
  248.         dstRun=dstBase+(srcHeight-(y+1))*dstPB;
  249.         for (x=0;x<srcWidth;x++) {
  250.             dstRun[0]=srcRun[0];
  251.             dstRun[1]=srcRun[1];
  252.             dstRun[2]=srcRun[2];
  253.             srcRun+=srcPB;
  254.             dstRun+=dstRB;
  255.         }
  256.     }
  257.     [self setImageRep:newRep];    
  258. }
  259.  
  260. - (void) rotateCCW:(id)sender {
  261.     NSBitmapImageRep* newRep;
  262.     unsigned char* srcBase;
  263.     unsigned char* dstBase;
  264.     unsigned char* srcRun;
  265.     unsigned char* dstRun;
  266.     long x,y,srcWidth,srcHeight,srcPB,dstPB,srcRB,dstRB;
  267.  
  268.     if (!imageRep) return;
  269.  
  270.     newRep=[[[NSBitmapImageRep alloc] initWithBitmapDataPlanes:NULL
  271.                                                     pixelsWide:[imageRep pixelsHigh]
  272.                                                     pixelsHigh:[imageRep pixelsWide]
  273.                                                  bitsPerSample:8
  274.                                                samplesPerPixel:3
  275.                                                       hasAlpha:NO
  276.                                                       isPlanar:NO
  277.                                                 colorSpaceName:NSDeviceRGBColorSpace
  278.                                                    bytesPerRow:0
  279.                                                   bitsPerPixel:0] autorelease];
  280.  
  281.     if (!newRep) return;
  282.  
  283.     //We go through the source Image row per row. Start
  284.  
  285.     srcBase=[imageRep bitmapData];
  286.     dstBase=[newRep bitmapData];
  287.  
  288.     srcWidth=[imageRep pixelsWide];
  289.     srcHeight=[imageRep pixelsHigh];
  290.  
  291.     srcPB=[imageRep bitsPerPixel]/8;
  292.     dstPB=[newRep bitsPerPixel]/8;
  293.  
  294.     srcRB=[imageRep bytesPerRow];
  295.     dstRB=[newRep bytesPerRow];
  296.  
  297.     for (y=0;y<srcHeight;y++) {
  298.         srcRun=srcBase+y*srcRB;
  299.         dstRun=dstBase+(srcWidth-1)*dstRB+y*dstPB;
  300.         for (x=0;x<srcWidth;x++) {
  301.             dstRun[0]=srcRun[0];
  302.             dstRun[1]=srcRun[1];
  303.             dstRun[2]=srcRun[2];
  304.             srcRun+=srcPB;
  305.             dstRun-=dstRB;
  306.         }
  307.     }
  308.     [self setImageRep:newRep];
  309. }
  310.  
  311. - (float) quality {
  312.     return quality;
  313. }
  314.  
  315. - (void) setQuality:(float)newQuality {
  316.     quality=newQuality;
  317.     if (quality<0.0f) quality=0.0f;
  318.     if (quality>1.0f) quality=1.0f;
  319. }
  320.  
  321. @end
  322.